<?php

namespace app\admin\controller;

use app\common\Common;
use app\common\BaseHelper as Helper;
use app\admin\model\Image;

use think\Request;

class Villageimages extends Base
{

    public function _initialize()
    {
        parent::_initialize();

        $this->defaultWhere = [
            'villageId' => $this->admin->villageId
        ];
        $this->pageSize = ['12', '24', '96'];
    }

    public function index(){

        return parent::index();
    }

    /**
     * Add
     */
    public function add(Request $request){

        return parent::add($request);
    }

    /**
     * Add Post
     */
    public function addPost(Request $request, $redirect = ''){
        $model = model($this->model);

        //save data
        if ($request->isAjax()) {
            $params = $request->param();

            if($params) {
                // Insert data
                $data['createOper'] = $this->admin->id;
                $data['villageId'] = $this->admin->villageId;

                if($params['imgUrl']){
                    foreach ($params['imgUrl'] as $key => $val) {
                        $data['id'] = Helper::getUUID();
                        $data['createDate'] = time();
                        $data['imgId'] = $params['imgIds'][$key];
                        $data['imgUrl'] = $val;
                        //dump($data);die;
                        $result = $model->create($data);

                        // Query执行后的操作
                        $model->_after_insert($data);
                        //Image::where('id', $data['imgId'])->update(['relatedId' => $data['id'], 'relatedTable' => 'Villageimages']);
                    }

                    if($result !== false){
                        // 写入日志
                        $logInfo = $this->admin->name . '添加了一条' . $this->model . '数据。';
                        common::adminLog($request, $logInfo);

                        return json(['status' => '1', 'msg' => 'success']);
                    }else{
                        return json(['status' => '0', 'msg' => 'error']);
                    }
                }
            }
            return json(['status' => '0', 'msg' => 'error']);
        }
    }


}